﻿// Scripts 文件
//功能说明：用户自定义桌面!
var draged = false;
var tdiv = null;
var dragObject = null;
var tempHtml = null;
var checkBox = null;

function CreateDiv(temp) {
    //保存状态
    var length = temp.length;
    if (length == 0) {
        //没有数据
        if (checkBox != null) checkBox.checked = false;
        alert('该模块未设置!');
    }
    else if (length == 1) {
        if (temp != "2")
            alert(temp == "1" ? "保存成功!" : "保存失败!");
        else //刷新页面
            window.location.reload();
        return;
    }
    else {
        var shadowObject1 = document.createElement("div");
        temp = temp.replace("params=", "params=" + GetSwfUrl() + "@");
        shadowObject1.innerHTML = temp;
        var tj = shadowObject1.children[0];
        shadowObject1 = null;
        for (var index = 0; index < tj.all.length; index++) {
            if (index >= 10)
                break;
            if (tj.all[index].tagName == "COMMENT") {
                tj.all[index - 1].innerHTML = tj.all[index].innerHTML;
                break;
            }
        }
        parentTable.cells[0].appendChild(tj);
    }
    //显示遮盖层
    ShowCover(false);
};

/*创建拖动对象阴影*/
function CloneElement(obj) {
    try {
        var shadowObject = document.createElement("div");
        shadowObject.style.display = "block";
        shadowObject.style.position = "absolute";
        shadowObject.style.filter = "alpha(opacity=50)";
        shadowObject.style.opacity = 50 / 100;
        shadowObject.style.cursor = "move";
        shadowObject.style.width = obj.offsetWidth;
        shadowObject.style.height = obj.offsetHeight;
        shadowObject.style.backgroundColor = "#58c7fb";
        shadowObject.style.borderStyle = "ridge double";
        shadowObject.style.borderColor = "#095bac";
        shadowObject.style.borderWidth = "1.5px";
        return shadowObject;
    }
    catch (e) {
        ErrorFinally();
    }
};

/*获取对象坐标*/
function GetPointXY(obj) {
    try {
        var to = new Object();
        to.left = to.right = to.top = to.bottom = 0;
        var twidth = obj.offsetWidth;
        var theight = obj.offsetHeight;
        //修改
        var scrollTop = document.body.scrollTop;
        while (obj != document.body) {
            to.left += obj.offsetLeft;
            to.top += obj.offsetTop;
            obj = obj.parentElement;
        }
        to.right = to.left + twidth;
        to.bottom = to.top + theight;
        return to;
    }
    catch (e) {
        ErrorFinally();
    }
};

/*开始拖拽*/
function dragStart() {
    dragObject = event.srcElement;

    if ((dragObject.tagName == "DIV") || (dragObject.tagName == "TR"))
        dragObject = dragObject.offsetParent;
    draged = true;

    tdiv = CloneElement(dragObject);
    var pointClass = GetPointXY(dragObject);
    tdiv.style.top = pointClass.top - 40;
    tdiv.style.left = pointClass.left - 15;
    document.body.appendChild(tdiv);

    lastX = event.clientX;
    lastY = event.clientY;
    lastLeft = tdiv.style.left;
    lastTop = tdiv.style.top;

    try {
        dragObject.dragDrop();
    }
    catch (e) {
        ErrorFinally();
    }
};

//重要:判断MOUSE的位置
function draging() {
    try {
        if (!draged)
            return;
        var tX = event.clientX;
        var tY = event.clientY;
        tdiv.style.left = parseInt(lastLeft) + tX - lastX;
        tdiv.style.top = parseInt(lastTop) + tY - lastY;
    }
    catch (e) {
        ErrorFinally();
    }
};

function dragEnd() {
    if (!draged) return;
    draged = false;
    endDrog();
    try {
        mm = ff(150, 15);
    }
    catch (e) {
        ErrorFinally();
    }
};

//从GOOGLE网站来,用于恢复位置
function ff(aa, ab) {
    var ac = parseInt(GetPointXY(tdiv).left);
    var ad = parseInt(GetPointXY(tdiv).top);
    var ae = (ac - GetPointXY(dragObject).left) / ab;
    var af = (ad - GetPointXY(dragObject).top) / ab;
    try {
        return setInterval(function () {
            if (ab < 1) {
                clearInterval(mm);
                tdiv.removeNode(true);
                dragObject = null;
                tdiv = null;
                return;
            }
            ab--;
            ac -= ae;
            ad -= af;
            tdiv.style.left = parseInt(ac) + "px";
            tdiv.style.top = parseInt(ad) + "px";
        }, aa / ab);
    }
    catch (e) {
        ErrorFinally();
    }
};

/*错误处理*/
function ErrorFinally() {
    if (tdiv != null) {
        tdiv.removeNode(true);
        tdiv = null;
    }
    if (dragObject != null)
        dragObject = null;
};

/*拖动结束后*/
function endDrog() {
    try {
        var tX = event.clientX;
        var tY = event.clientY + document.body.scrollTop;
        for (var i = 0; i < parentTable.cells.length; i++) {
            var parentCell = GetPointXY(parentTable.cells[i]);
            var partentTableSub = parentTable.cells[i];
            if (tX >= parentCell.left && tX <= parentCell.right) {
                if (partentTableSub.children.length == 0) {
                    partentTableSub.appendChild(dragObject);
                    break;
                }
                for (var index = 0; index < partentTableSub.children.length; index++) {
                    var subTables = partentTableSub.children[index];
                    var subTable = GetPointXY(subTables);
                    if (tY <= subTable.top) {
                        partentTableSub.insertBefore(dragObject, subTables);
                        break;
                    }
                    if (index + 1 == partentTableSub.children.length) {
                        partentTableSub.appendChild(dragObject);
                        break;
                    }
                }
            }
        }
    }
    catch (e) {
        ErrorFinally();
    }
};

/*自定义桌面*/
/*点击自定义桌面按钮时*/
function SelfDesk(obj) {
    //判断是否为第一个功能模块，如果是则让用户先选择布局模式

    if (parentTable.cells.length == 0) {
        //显示遮盖层
        var layoutMode = Object$('layoutMode');
        ShowCover(true);
        layoutMode.style.display = "";
        layoutMode.style.top = (document.documentElement.clientHeight - layoutMode.offsetHeight) / 2;
        layoutMode.style.left = (document.documentElement.clientWidth - layoutMode.offsetWidth) / 2;
        return;
    }

    var SelfSelectDiv = Object$('SelfSelectDiv');

    SelfSelectDiv.style.display = "";
    SelfSelectDiv.style.top = (document.documentElement.clientHeight - SelfSelectDiv.offsetHeight) / 2;
    SelfSelectDiv.style.left = (document.documentElement.clientWidth - SelfSelectDiv.offsetWidth) / 2;

    var Btn_Cancel = Object$('Btn_Cancel');
    Btn_Cancel.style.display = "";

    var modelBox = Object$('modelBox');
    modelBox.style.height = SelfSelectDiv.offsetHeight - 60;
};

/*选择设置好的桌面*/
function SelectDesk(obj) {
    //显示遮盖层
    ShowCover(true);
    var SelectDeskWindows = Object$('SelectDeskWindows');
    var topPx = document.body.scrollTop;
    SelectDeskWindows.style.top = (document.body.clientHeight - SelectDeskWindows.offsetHeight) / 2 + topPx;
    SelectDeskWindows.style.left = (document.body.clientWidth - SelectDeskWindows.offsetWidth / 2) / 2;
    SelectDeskWindows.style.display = "";
};

/*遮盖层方法*/
function ShowCover(flag) {
    var cover = Object$('cover');
    if (flag) {
        cover.style.backgroundColor = "#999999";
        cover.style.display = "";
        cover.style.filter = "alpha(opacity=50)";
        cover.style.opacity = 50 / 100;
        cover.style.height = document.body.scrollHeight;
        return;
    }
    cover.style.display = "none";
};

/*模式选择*/
function SelectModel(obj) {
    var newTr = parentTable.insertRow();
    var newTd0 = newTr.insertCell(0);
    var newTd1 = newTr.insertCell(1);
    newTd0.vAlign = "top";
    newTd1.vAlign = "top";
    if (obj.id == "model_2") //左中右
    {
        var newTd2 = newTr.insertCell(2);
        newTd0.style.width = "25%";
        newTd1.style.width = "50%";
        newTd2.style.width = "25%";
        newTd2.vAlign = "top";
    }
    else {
        newTd0.style.width = "50%";
        newTd1.style.width = "50%";
    }

    var layoutMode = Object$('layoutMode');
    var cover = Object$('cover');
    cover.style.display = "none";
    layoutMode.style.display = "none";

    var SelfSelectDiv = Object$('SelfSelectDiv');
    SelfSelectDiv.style.display = "";
    SelfSelectDiv.style.top = (document.documentElement.clientHeight - SelfSelectDiv.offsetHeight) / 2;
    SelfSelectDiv.style.left = (document.documentElement.clientWidth - SelfSelectDiv.offsetWidth) / 2;
    var Btn_Save = Object$('Btn_Save');
    var Btn_Cancel = Object$('Btn_Cancel');
    Btn_Cancel.style.display = "";
    var modelBox = Object$('modelBox');
    modelBox.style.height = SelfSelectDiv.offsetHeight - 60;
};

/*返回对象*/
function Object$(_objstr) {
    var obj = document.getElementById(_objstr);
    if (obj != null)
        return obj
};

/*取消*/
function BtnCancelClick() {
    Object$('SelfSelectDiv').style.display = "none";
    //隐藏布局模式窗
    Object$("layoutMode").style.display = "none";
    Object$("cover").style.display = "none";
};

/*自定义桌面部分*/
function ShowDeskUser(xmlStr) {
    //根据屏幕宽度、高度设置自定义桌面容器得宽度、高度
    if (xmlStr == "")
        return;
    var domDocument = new ActiveXObject("Msxml2.DOMDocument.3.0");
    try {
        domDocument.loadXML(xmlStr);
        //将用户能操作的功能模块添加到页面
        //将用户所属单位级别下设置的桌面信息添加到页面上
        FillDeskSelect(FillData(domDocument));
    }
    catch (e) {
    }
};

/*填充桌面选择*/
function FillDeskSelect(strXml) {
    var Drop_Desk = Object$('Drop_Desk');
    var array = strXml.split("|");
    for (var index = 0; index < array.length; index++) {
        var _text = array[index].split(",")[0];
        var _value = array[index].split(",")[1];
        Drop_Desk.options[Drop_Desk.options.length] = new Option(_text, _text);
    }
    Drop_Desk.value = "0";
};

/*将数据填充到容器中*/
function FillData(xml) {
    var Xpath = "/Workbook/nodeParent/nodeChild";
    var _xml = xml.documentElement.selectNodes(Xpath); //.childNodes.length;

    var modelBox = Object$("modelBox");
    //生成一级菜单
    for (var index = 0; index < _xml.length; index++) {
        var url = _xml[index].attributes[2].nodeValue;
        for (var model = 0; model < _xml[index].childNodes.length; model++) {
            var id = _xml[index].childNodes[model].attributes[0].nodeValue;
            var state = _xml[index].childNodes[model].attributes[1].nodeValue; //状态
            var modid = _xml[index].childNodes[model].attributes[2].nodeValue; //名称
            var name = _xml[index].childNodes[model].attributes[3].nodeValue; //名称
            var loadUrl = _xml[index].childNodes[model].attributes[4].nodeValue; //加载路径
            var divBox = "<div id=" + id + " style='margin:5px;text-align:left;width:90%;height:20px;border:1px gray solid;'>";
            divBox += name + "<input id='chk_" + id + "' onclick='CheckBoxOnClick(this);' type='checkbox'/></div>";
            modelBox.innerHTML += divBox;
            var chkBox = Object$("chk_" + id);
            chkBox.checked = state == "1";
            chkBox.LoadUrl = loadUrl;
            chkBox.Caption = name;
            chkBox.modid = modid;
        }
    }
    return xml.documentElement.selectNodes("/Workbook/DeskSelect")[0].attributes[0].nodeValue;
};

/*点击多选框时*/
function CheckBoxOnClick(obj) {
    //显示遮盖层
    ShowCover(true);
    if (!obj.checked) {
        //显示遮盖层
        ShowCover(false);
        var objDiv = "Div_" + obj.id.replace("chk_", "");
        objDiv = Object$(objDiv);
        if (objDiv != null) {
            objDiv.parentNode.removeChild(objDiv);
            objDiv = null;
        }
        ShowCover(false);
        checkBox = null;
    }
    else {
        checkBox = obj;
        var objDiv = "Div_" + obj.id.replace("chk_", "");
        var divModel = new DivModel(obj);
        GetReportId("0##" + divModel.config);
    }
};

var DivModel = function (obj) {
    this.id = obj.id;
    this.state = obj.checked;
    this.loadUrl = obj.LoadUrl;
    this.name = obj.Caption;
    this.modid = obj.modid;
    this.config = this.id.replace("chk_", "") + "," + this.loadUrl + "," + this.state + "," + this.name + "," + this.modid;
};

/*保存*/
function BtnSaveClick() {
    var strSave = "";
    var str = "";
    for (var pIndex = 0; pIndex < parentTable.cells.length; pIndex++) {
        var childCount = parentTable.cells[pIndex].children.length;
        var childNode = parentTable.cells[pIndex].children;
        var index = 0;
        for (var childIndex = 0; childIndex < childCount; childIndex++) {
            var control = childNode[childIndex];
            if (control.className != "dragTable")
                continue;
            var controlModel = new ControlModel(control, (pIndex + 1), index++);
            str += "##" + controlModel.config;
        }
    }
    strSave = "1##" + (pIndex) + str;
    GetReportId(strSave);
};

var ControlModel = function (obj, pIndex, index) {
    this.loadUrl = obj.LoadUrl; //路径
    this.id = obj.id; //所属ID
    this.index = index; //排序号
    this.layout = pIndex; //所在区域（布局）

    this.config = this.id.replace("Div_", "") + "," + this.index + "," + this.layout + "," + this.loadUrl;
};

/*
初始化
标题栏移动：(模式选择标题栏、内容选择标题栏)
*/
var titleX, titleY;
var MoveTitle = "";

if (window.attachEvent) {
    document.attachEvent("onmouseup", TitleMouseUp);
} else if (window.addEventListener) {
    document.addEventListener("mouseup", TitleMouseUp, false);
};

document.onmousemove = TitleMove;

function TitleDragStart(obj) {
    MoveTitle = obj.id;
    document.all(MoveTitle).setCapture();
    titleX = event.x - document.all(MoveTitle).style.pixelLeft;
    titleY = event.y - document.all(MoveTitle).style.pixelTop;
};

function TitleMove() {
    if (MoveTitle != "") {
        document.all(MoveTitle).style.left = event.x - titleX;
        document.all(MoveTitle).style.top = event.y - titleY;
    }
};
function TitleMouseUp() {
    if (MoveTitle != "") {
        document.all(MoveTitle).releaseCapture();
        MoveTitle = "";
    }
};

/*弹出右键菜单*/
function ShowPopMenu(event) {
    /*判断是否响应右键菜单*/
    var cover = Object$('cover');
    if (cover.style.display == "")
        return;
    var topper2 = event.clientY + document.body.scrollTop;
    var lefter2 = event.clientX;

    var this_win = document.body;
    var PopMenu = Object$('PopMenu');

    PopMenu.style.display = "";
    PopMenu.style.top = topper2 + "px";
    PopMenu.style.left = lefter2 + "px";
};

/*清空桌面*/
function ClearDesk() {
    var SelfSelectDiv = Object$("SelfSelectDiv");

    var modelBox = Object$("modelBox");

    Object$("modelBox").children.length;

    //显示遮盖层
    ShowCover(true);
    var parentTable = Object$("parentTable");
    var index = 0;
    for (index; index < parentTable.children.length; index++) {
        var tr = parentTable.children[index];
        try {
            tr.parentNode.removeChild(tr);
            tr = null;
        }
        catch (e) {
        }
    }
    for (index = 0; index < modelBox.children.length; index++) {
        var childMenu = modelBox.children[index];
        var checkBox = childMenu.children[0];
        if (checkBox.checked)
            checkBox.checked = false;
    }
    ShowCover(false);
};

//当对弹出菜单进行操作后,关闭弹出菜单
function ClosePopMenu() {
    var PopMenu = Object$('PopMenu');
    PopMenu.style.display = "none";
};

/*获取当前WEB页面的路径 flash曲线使用*/
function GetSwfUrl() {
    return document.URLUnencoded.replace(document.forms[0].action, "");
};

/*去Flash 虚框*/
function pswf(tobj) {
    if (typeof (tobj) == "undefined" || tobj == null)
        return;
    if (tobj.text != undefined)
        document.write(tobj.text);
};